﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Xceed.Silverlight.DataGrid
{
  public class DataGridBindingInfo
  {
    internal DataGridBindingInfo( string path, bool readOnly, bool autoGenerated )
      : this( path )
    {
      this.ReadOnly = readOnly;

      m_autoGenerated = autoGenerated;
    }

    public DataGridBindingInfo()
    {
      m_binding = new Binding();

      m_binding.Mode = BindingMode.OneWay;
      m_binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.Default;
      m_binding.ConverterCulture = CultureInfo.CurrentCulture;

      m_binding.ValidatesOnDataErrors = true;
      m_binding.ValidatesOnExceptions = true;

#if SILVERLIGHT 

      m_binding.ValidatesOnNotifyDataErrors = true;

#endif

      m_binding.NotifyOnValidationError = true;
    }

    public DataGridBindingInfo( string path )
      : this()
    {
      this.Path = new PropertyPath( path );
    }

    #region IsAutoGenerated Property

    internal bool IsAutoGenerated
    {
      get
      {
        return m_autoGenerated;
      }
    }

    #endregion

    #region BindsDirectlyToSource Property

    public bool BindsDirectlyToSource
    {
      get
      {
        return m_binding.BindsDirectlyToSource;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.BindsDirectlyToSource = value;
      }
    }

    #endregion BindsDirectlyToSource Property

    #region Converter Property

    public IValueConverter Converter
    {
      get
      {
        return m_binding.Converter;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.Converter = value;
      }
    }

    #endregion Converter Property

    #region ConverterCulture Property

    public CultureInfo ConverterCulture
    {
      get
      {
        return m_binding.ConverterCulture;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.ConverterCulture = value;
      }
    }

    #endregion ConverterCulture Property

    #region ConverterParameter Property

    public object ConverterParameter
    {
      get
      {
        return m_binding.ConverterParameter;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.ConverterParameter = value;
      }
    }

    #endregion ConverterParameter Property

    #region ElementName Property

    public string ElementName
    {
      get
      {
        return m_binding.ElementName;
      }
      set
      {
        m_binding.ElementName = value;
      }
    }

    #endregion ElementName Property

    #region FallbackValue Property

    public object FallbackValue
    {
      get
      {
        return m_binding.FallbackValue;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.FallbackValue = value;
      }
    }

    #endregion FallbackValue Property

    #region Path Property

    [TypeConverter( typeof( PropertyPathConverter ) )]
    public PropertyPath Path
    {
      get
      {
        return m_binding.Path;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.Path = value;
      }
    }

    #endregion Path Property

    #region ReadOnly Property

    public bool ReadOnly
    {
      get
      {
        return ( m_binding.Mode == BindingMode.OneWay );
      }
      set
      {
        m_autoGenerated = false;

        if( value )
        {
          m_binding.Mode = BindingMode.OneWay;
        }
        else
        {
          m_binding.Mode = BindingMode.TwoWay;
        }
      }
    }

    #endregion ReadOnly Property

    #region StringFormat Property

    public string StringFormat
    {
      get
      {
        return m_binding.StringFormat;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.StringFormat = value;
      }
    }

    #endregion StringFormat Property

    #region TargetNullValue Property

    public object TargetNullValue
    {
      get
      {
        return m_binding.TargetNullValue;
      }
      set
      {
        m_autoGenerated = false;

        m_binding.TargetNullValue = value;
      }
    }

    #endregion //TargetNullValue Property

    internal Binding GetBinding()
    {
      return m_binding;
    }

    private Binding m_binding;
    private bool m_autoGenerated;
  }
}
